//
//  ViewController.m
//  QQ音乐(皇家尊享版)
//
//  Created by 周冬冬 on 16/7/19.
//  Copyright © 2016年 我的mac. All rights reserved.
//

#import "ViewController.h"
#import "ZDDMusic.h"
#import "YYModel.h"
#import <Masonry/Masonry.h>
#import "ZDDMusicManger.h"
#import "ZDDLyricView.h"
#import "ZDDColorLabel.h"
#import "ZDDLynic.h"
#import "ZDDMusicParser.h"

@interface ViewController ()<ZDDLyricViewDelegate>
//所有音乐
@property(nonatomic,strong)NSArray <ZDDMusic *> * allMusic;
//当前音乐的索引
@property (nonatomic, assign) NSInteger currentMusicIndex;
//当前歌曲的歌词模型数组
@property (nonatomic, strong) NSArray <ZDDLynic *> *currentLyricModels;
//当前歌词的索引
@property (nonatomic, assign) NSInteger currentLyricIndex;



#pragma mark - 公用视图
//背景视图
@property (weak, nonatomic) IBOutlet UIImageView *bgView;
//当前时间
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
//持续时间
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
//进度条
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
//上一曲
@property (weak, nonatomic) IBOutlet UIButton *preButton;
//下一曲
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
//播放
@property (weak, nonatomic) IBOutlet UIButton *playButton;


#pragma mark - 竖屏视图
//专辑名
@property (weak, nonatomic) IBOutlet UILabel *albumLabel;
//专辑图片
@property (weak, nonatomic) IBOutlet UIImageView *albumImgV;
//歌手
//歌词视图
@property (weak, nonatomic) IBOutlet ZDDLyricView *lyricView;
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
@property (weak, nonatomic) IBOutlet UIView *vCenterView;
//歌词
@property (weak, nonatomic) IBOutlet ZDDColorLabel *lyricLabel;

#pragma mark - 横屏视图
//横向专辑图片
@property (weak, nonatomic) IBOutlet UIImageView *hAlbumImgV;
//横向歌词
@property (weak, nonatomic) IBOutlet ZDDColorLabel *hLyricLabel;
//更新定时器
@property (nonatomic, strong) NSTimer *updateTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //设置封面图
    [self prepareAlbumImgV];
    [self allMusic];
    //设置数据
    [self prepareData];
    //设置背景视图
    [self prepareBgView];

    self.lyricView.delegate = self;
    
    
}
#pragma mark - 懒加载
-(NSArray <ZDDMusic *> *)allMusic{

    //从plist中取出数据
    if (_allMusic == nil) {
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"mlist.plist" ofType:nil];
        
        NSArray * musicArr = [NSArray arrayWithContentsOfFile:filePath];
        //数据的转换
        _allMusic =[NSArray yy_modelArrayWithClass:[ZDDMusic class] json:musicArr];
    }


    return _allMusic;

}
//设置数据
-(void)prepareData{

    //取出数据
    ZDDMusic * music = self.allMusic[self.currentMusicIndex];
    //设置背景图片
    self.bgView.image = [UIImage imageNamed:music.image];
    //设置专辑名
    self.albumLabel.text = music.album;
    //设置专辑图片
    self.albumImgV.image = [UIImage imageNamed:music.image];
    //设置歌手
    self.singerLabel.text = music.singer;
    //设置横向专辑图片
    self.hAlbumImgV.image = [UIImage imageNamed:music.image];
    
    //设置默认播放
    [self clickPlayBtn:self.playButton];
    
    

    self.durationLabel.text = [self timeStrWithTimeinterval:[ZDDMusicManger sharedManger].duration];
    //设置歌词
    self.currentLyricModels = [ZDDMusicParser parserLyricFilename:music.lrc];
    //设置歌词模型数组
    self.lyricView.lyricModels = self.currentLyricModels;


}
-(void)updateLyric{

    //取出当前的歌词
    ZDDLynic * currentLyric = self.currentLyricModels[self.currentLyricIndex];
    //取出下一句歌词
    ZDDLynic * nextLyric;
    if (self.currentLyricIndex == self.currentLyricModels.count - 1) {
        nextLyric = [[ZDDLynic alloc] init];
        nextLyric.content = currentLyric.content;
        //设置次序时间
        nextLyric.initialTime = [ZDDMusicManger sharedManger].duration;
    }else{
    
        nextLyric = self.currentLyricModels[self.currentLyricIndex + 1];
    
    
    
    }

    if ([ZDDMusicManger sharedManger].currentTime > nextLyric.initialTime && (self.currentLyricIndex < self.currentLyricModels.count - 1)) {
        self.currentLyricIndex++;
        //方法循环  直到当前索引为正确的索引
        [self updateLyric];
        return;
    }
    if ([ZDDMusicManger sharedManger].currentTime < currentLyric.initialTime && (self.currentLyricIndex > 0)) {
        self.currentLyricIndex--;
        //方法循环  直到当前索引为正确的索引
        [self updateLyric];
        return;
    }
    //设置数据
    self.lyricLabel.text = self.currentLyricModels[self.currentLyricIndex].content;
    self.hLyricLabel.text = self.currentLyricModels[self.currentLyricIndex].content;
     CGFloat progress = ([ZDDMusicManger sharedManger].currentTime - currentLyric.initialTime) / (nextLyric.initialTime - currentLyric.initialTime);
    self.lyricLabel.progress = progress;
    self.hLyricLabel.progress = progress;
    //设置歌词视图的索引
    self.lyricView.lyricIndex = self.currentLyricIndex;
    //设置歌词视图的进度
    self.lyricView.lyricProgress = progress;

    



}
//更新数据
-(void)updata{

    //设置当前的时间
    self.currentLabel.text = [self timeStrWithTimeinterval:[ZDDMusicManger sharedManger].currentTime];
//封面图片的旋转
    self.albumImgV.transform = CGAffineTransformRotate(self.albumImgV.transform, M_PI_2 * 0.02);
    //进度条的移动
    self.progressSlider.value = [ZDDMusicManger sharedManger].currentTime / [ZDDMusicManger sharedManger].duration;
    if ([self.currentLabel.text isEqualToString:self.durationLabel.text]) {
        [self clickNextBtn:self.nextButton];
    }

    //更新歌词
    [self updateLyric];

}
//转换时间间隔为字符串
-(NSString *)timeStrWithTimeinterval:(NSTimeInterval)timeinterval{


    int minute = timeinterval / 60;
    int second = (int)timeinterval % 60;
    
    return [NSString stringWithFormat:@"%02d:%02d",minute,second];


}
//播放按钮
- (IBAction)clickPlayBtn:(id)sender {
    //获取数据
    ZDDMusic * music = self.allMusic[self.currentMusicIndex];
    //暂停判断
    if (self.playButton.selected) {
        [[ZDDMusicManger sharedManger] pauseMusic];
        self.playButton.selected = NO;
        //停止定时器
        [self.updateTimer invalidate];
    }else{
    //播放
        [[ZDDMusicManger sharedManger] playMusicWithFilename:music.mp3];
        self.playButton.selected = YES;
        //更新数据
        self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updata) userInfo:nil repeats:YES];
        //设置runloop
        [[NSRunLoop currentRunLoop] addTimer:self.updateTimer forMode:NSRunLoopCommonModes];
    
    }
    
}
//下一曲按钮
- (IBAction)clickNextBtn:(id)sender {
    //设置索引
    if (self.currentMusicIndex == self.allMusic.count - 1) {
        self.currentMusicIndex = 0;
    }else{
    
        self.currentMusicIndex ++;
    }
    //设置选中状态为NO
    self.playButton.selected = NO;
    //停止定时器
    [self.updateTimer invalidate];

    //设置数据
    [self prepareData];
    
}
//上一曲按钮
- (IBAction)clickpreBtn:(id)sender {
    
    if (self.currentMusicIndex == 0) {
        self.currentMusicIndex = self.allMusic.count - 1;
    }else{
    
        self.currentMusicIndex --;
    }
    //设置选中状态为NO
    self.playButton.selected = NO;
    //停止定时器
    [self.updateTimer invalidate];

    //设置数据
    [self prepareData];

}
//调整进度

- (IBAction)clickProgressSlider:(UISlider *)sender {
    
    [ZDDMusicManger sharedManger].currentTime = sender.value * [ZDDMusicManger sharedManger].duration;
    
}

//设置背景视图
-(void)prepareBgView{

    //设置视觉特效
    UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    //创建特效
    UIVisualEffectView * effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    //将特效视图添加到目标视图
    [self.bgView addSubview:effectView];
    //设置位置  沾满整个视图
    [effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    

}
-(void)hScrollViewWithProgress:(CGFloat)progress{

    self.vCenterView.alpha = 1 - progress;




}
//设置封面视图
- (void)prepareAlbumImgV{
    //设置裁切
    self.albumImgV.layer.cornerRadius = 100;
    self.albumImgV.layer.masksToBounds = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
